package com.example.bootdemo.config;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class DateUtils {

    public static Date parseDate(String source, String[] parsePatterns) {
        if (source == null || source.length() == 0) {
            return null;
        }
        ParsePosition pos = new ParsePosition(0);
        Date date = null;
        for (String parsePattern: parsePatterns) {
            if (date == null) {
                date = new SimpleDateFormat(parsePattern).parse(source, pos);
            } else {
                date = new SimpleDateFormat(parsePattern, Locale.getDefault()).parse(source, pos);
            }
        }
        return date;
    }
    public static String format(Date date, String[] formatPatterns) {
        if (date == null) {
            return "";
        }
        return new SimpleDateFormat(formatPatterns[0], Locale.getDefault()).format(date);
    }
}
